/*
 * @Descripttion: 
 * @version: 
 * @Author: 
 * @Date: 2022-04-24 09:01:49
 * @LastEditors: weidelong
 * @LastEditTime: 2022-05-09 14:25:54
 */
const express = require('express')
const social = require('../db/socialSchema_mobile')
const router = express.Router()

// 发布动态
router.post('/releaseGraphic', (req, res) => {
	const params = req.body
	social.create(params, (err, result) => {
		if (err) {
			throw error
		} else {
			res.send({
				status: 200,
				data: {
					code: 0,
				},
				msg: '发布成功'
			})
		}
	})
})

// 获取动态
router.get('/getGraphicList', async (req, res) => {
	const list = await social.find({}).sort({ _id: -1 })
	console.log(list)
	if (list) {
		res.send({
			status: 200,
			data: {
				code: 0,
				list
			},
			msg: '查询成功'
		})
	} else {
		res.send({
			status: 500,
			msg: '查询失败'
		})
	}
})


// 评论
router.post('/sendComments', (req, res) => {
	const params = {
		sender: req.body.sender,
		content: req.body.content
	}
	const { id } = req.body
	social.findOne({id}).exec(function(err,data){
		// 先查询到指定的数据data
		// 然后修改数据
		if (data) {
			data.pinglun.push(params)
			data.save(function(err){
				// err在修改错误时报错有值
				console.log(err)
				if (err) {
					res.send({
						status: 500,
						msg: '评论失败'
					})
				} else {
					res.send({
						status: 200,
						data: {
							code: 0,
						},
						msg: '评论成功'
					})
				}
			})
		}	
	})
})

module.exports = router